 package org.code.dbconn;
 
 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.LinkedList;
 import java.util.Map;
import org.code.util.Prop;
 
 public class MyDataSource
 {
   private static String url = "";
   private static String user = "";
   private static String password = "";
   private static String dbClassName = "";
   private static String propFileName = "createcode.properties";
   private static int initCount = 0;
   private static int maxCount = 0;
   private int currentCount = 0;
 
   LinkedList<Connection> connectionsPool = new LinkedList<Connection>();
 
   public MyDataSource()
   {
     try
     {
       Prop prop = new Prop();
       Map<String, String> propMap = prop.getProp(propFileName);
       dbClassName = (String)propMap.get("jdbc.driverClassName");
       url = (String)propMap.get("jdbc.url");
       user = (String)propMap.get("jdbc.username");
       password = (String)propMap.get("jdbc.password");
       initCount = Integer.valueOf((String)propMap.get("jdbc.minIdle")).intValue();
       maxCount = Integer.valueOf((String)propMap.get("jdbc.initialSize")).intValue();
     }
     catch (Exception e)
     {
       e.printStackTrace();
     }
 
     try
     {
    	System.out.print(dbClassName+"  "+initCount);
       Class.forName(dbClassName);
       for (int i = 0; i < initCount; i++) {
         this.connectionsPool.addLast(createConnection());
         this.currentCount += 1;
       }
     } catch (SQLException e) {
       throw new ExceptionInInitializerError(e);
     }
     catch (ClassNotFoundException e) {
       e.printStackTrace();
     }
   }
 
   public Connection getConnection() throws SQLException {
     synchronized (this.connectionsPool) {
       if (this.connectionsPool.size() > 0) {
         return (Connection)this.connectionsPool.removeFirst();
       }
       if (this.currentCount < maxCount) {
         this.currentCount += 1;
         return createConnection();
       }
 
       throw new SQLException(" has no connection");
     }
   }
 
   public void free(Connection conn) {
     this.connectionsPool.addLast(conn);
   }
   
   public String getDB2listTables(){
	   return "select   tabname   from   syscat.tables   where   tabschema   =   current   schema ";  
   }
 
   private Connection createConnection() throws SQLException {
     return DriverManager.getConnection(url, user, password);
   }
 
   public static void main(String[] args)
     throws SQLException
   {
     ConnectionPond.getInstance(); MyDataSource mds = ConnectionPond.getMds();
 
     Connection conn = mds.getConnection();
 
     String sql = "show tables";
 
     PreparedStatement ps = conn.prepareStatement(sql);
 
     ResultSet rs = ps.executeQuery();
 
     while (rs.next())
     {
       System.out.println(rs.getString(1));
     }
 
     mds.free(conn);
   }
 }

